<%
'#################################################################################
'## UnWritten Open Source Blog Engine
'#################################################################################
'## Copyright (C) 2008 Luigi Violin
'##
'## This program is free software: you can redistribute it and/or modify
'## it under the terms of the GNU General Public License as published by
'## the Free Software Foundation, either version 2 of the License, or
'## (at your option) any later version.
'##
'## This program is distributed in the hope that it will be useful,
'## but WITHOUT ANY WARRANTY; without even the implied warranty of
'## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'## GNU General Public License for more details.
'##
'## You should have received a copy of the GNU General Public License
'## along with this program.  If not, see <http://www.gnu.org/licenses/>.
'##
'## You can obtain support from our forums at:
'##     <http://www.unwrittenblog.com/forums/>
'## Contact the author directly:
'##     <zaamit@hotmail.com/>
'##
'#################################################################################
%>
<!--#include virtual="/config.asp"-->
<!--#include virtual="/includes/core-functions.asp"-->
<%
'open db connection
Dim dbConn,dbRS,dbRS2,opp
set dbConn = Server.CreateObject("ADODB.Connection")
set dbRS = Server.CreateObject("ADODB.RecordSet")
set dbRS2 = Server.CreateObject("ADODB.RecordSet")
dbConn.open "Driver={MySQL ODBC 3.51 Driver};Server=" & cfg_serverip & ";Port=" & cfg_port & ";Database=" & cfg_db & ";User=" & cfg_user & ";Password=" & cfg_pass & ";Option=3;"
opp = 10
randomize()

Dim ptype,page,host,elem
ptype = request.QueryString("type")
page = request.QueryString("page")
host = request.QueryString("host")
elem = request.QueryString("elem")

'table header
select case ptype
	case 1
	'published posts list
		strsql = "select * from " & db_prefix & "posts where ent_type = 1 and publish_status = 1 order by pdate desc"
		'coulmns heading
		tableH "Post title","Posted","Views","Publish&nbsp;Status","&nbsp;"
	case 2
	'unpublished posts list
		strsql = "select * from " & db_prefix & "posts where ent_type = 1 and publish_status <> 1 order by pdate desc"
		'coulmns heading
		tableH "Post title","Posted","Views","Publish&nbsp;Status","&nbsp;"
	case 3
	'comments
		strsql = "select " & db_prefix & "comments.*," & db_prefix & "posts.ent_name," & db_prefix & "posts.title from " & db_prefix & "comments," & db_prefix & "posts where postid = " & db_prefix & "posts.id order by date desc"
		tableH "Author","Posted","Content","Publish&nbsp;Status","&nbsp;"
	case 4
	'published articles
		strsql = "select * from " & db_prefix & "posts where ent_type = 2 and publish_status = 1 and ent_name not in ('home','about','contact') order by pdate desc"
		tableH "Article Name","Posted","Views","Publish&nbsp;Status","&nbsp;"
	case 5
	'unpublished articles
		strsql = "select * from " & db_prefix & "posts where ent_type = 2 and publish_status <> 1 and ent_name not in ('home','about','contact') order by pdate desc"
		tableH "Article Name","Posted","Views","Publish&nbsp;Status","&nbsp;"
	case 6
	'system pages
		strsql = "select * from " & db_prefix & "posts where ent_type = 2 and ent_name in ('home','about','contact') order by pdate desc"
		tableH "Article Name","Posted","Views","Publish&nbsp;Status","&nbsp;"
	case 7
	'published polls
		strsql = "select * from " & db_prefix & "polls where status = 1 and expire_date > '" & str2dat(now()) & "' order by date desc"
		tableH "Poll","Posted","Expires","Publish&nbsp;Status","&nbsp;"
	case 8
	'unpublished polls
		strsql = "select * from " & db_prefix & "polls where status <> 1 and expire_date > '" & str2dat(now()) & "' order by date desc"
		tableH "Poll","Posted","Expires","Publish&nbsp;Status","&nbsp;"
	case 9
	'expired polls
		strsql = "select * from " & db_prefix & "polls where expire_date < '" & str2dat(now()) & "' order by date desc"
		tableH "Poll","Posted","Expired","Publish&nbsp;Status","&nbsp;"
end select

strsql = strsql & " limit " & (page-1)*opp & "," & opp
'response.Write strsql
dbRS.open strsql,dbConn
if dbRS.eof then
	Response.Write "<tr><td colspan=""5""><i>No content found</i></td></tr>"
end if

'table content
select case ptype
	case 1
	'published posts list
		do while not(dbRS.eof)
			Response.Write "<tr><td><a href=""/?pg=" & dbRS("ent_name") & """>" & dbRS("title") & "</a></td>"
			Response.Write "<td>" & dat2str(dbRS("pdate")) & "</td>"
			Response.Write "<td>" & dbRS("views") & "</td>"
			Response.Write "<td>" & getpstat(dbRS("publish_status")) & "</td>"
			Response.Write "<td><a href=""edit.asp?id=" & dbRS("id") & """>Edit</td></tr>"
			dbRS.movenext
		loop
	case 2
	'published posts list
		do while not(dbRS.eof)
			Response.Write "<tr><td><a href=""/?pg=" & dbRS("ent_name") & """>" & dbRS("title") & "</a></td>"
			Response.Write "<td>" & dat2str(dbRS("pdate")) & "</td>"
			Response.Write "<td>" & dbRS("views") & "</td>"
			Response.Write "<td>" & getpstat(dbRS("publish_status")) & "</td>"
			Response.Write "<td><a href=""edit.asp?id=" & dbRS("id") & """>Edit</td></tr>"
			dbRS.movenext
		loop
	case 3
	'comments list
		do while not(dbRS.eof)
			Response.Write "<tr>"
			if dbRS("author_id") > 0 then
				Response.Write "<td><a href=""/admin/user-edit.asp?id=" & dbRS("author_id") & """>" & dbRS("author_name") & "</a></td>"
				else
					Response.Write "<td><strong>" & dbRS("author_name") & "</strong></td>"
			end if
			Response.Write "<td>" & dat2str(dbRS("date")) & "</td>"
			Response.Write "<td>" & dbRS("text") & "</td>"
			Response.Write "<td>" & getpstat(dbRS("status")) & "</td>"
			Response.Write "<td><a href=""comment-edit.asp?id=" & dbRS("id") & """>Edit</td>"
			Response.Write "</tr>"
			dbRS.movenext
		loop
	case 4
	'published articles
		do while not(dbRS.eof)
			Response.Write "<tr>"
			Response.Write "<td><a href=""/?pg=" & dbRS("ent_name") & """>" & dbRS("title") & "</a></td>"
			Response.Write "<td>" & dat2str(dbRS("pdate")) & "</td>"
			Response.Write "<td>" & dbRS("views") & "</td>"
			Response.Write "<td>" & getpstat(dbRS("publish_status")) & "</td>"
			Response.Write "<td><a href=""edit.asp?id=" & dbRS("id") & """>Edit</td>"
			Response.Write "</tr>"
			dbRS.movenext
		loop
	case 5
	'unbublished articles
		do while not(dbRS.eof)
			Response.Write "<tr>"
			Response.Write "<td><a href=""/?pg=" & dbRS("ent_name") & """>" & dbRS("title") & "</a></td>"
			Response.Write "<td>" & dat2str(dbRS("pdate")) & "</td>"
			Response.Write "<td>" & dbRS("views") & "</td>"
			Response.Write "<td>" & getpstat(dbRS("publish_status")) & "</td>"
			Response.Write "<td><a href=""edit.asp?id=" & dbRS("id") & """>Edit</td>"
			Response.Write "</tr>"
			dbRS.movenext
		loop
	case 6
	'system pages
		do while not(dbRS.eof)
			Response.Write "<tr>"
			Response.Write "<td><a href=""/?pg=" & dbRS("ent_name") & """>" & dbRS("title") & "</a></td>"
			Response.Write "<td>" & dat2str(dbRS("pdate")) & "</td>"
			Response.Write "<td>" & dbRS("views") & "</td>"
			Response.Write "<td>" & getpstat(dbRS("publish_status")) & "</td>"
			Response.Write "<td><a href=""edit.asp?id=" & dbRS("id") & """>Edit</td>"
			Response.Write "</tr>"
			dbRS.movenext
		loop
	case 7
	'published polls
		do while not(dbRS.eof)
			Response.Write "<tr>"
			Response.Write "<td><a href=""javascript:showhidePR(" & dbRS("id") & ");"">" & dbRS("title") & "</a></td>"
			Response.Write "<td>" & dat2str(dbRS("date")) & "</td>"
			Response.Write "<td>" & (dbRS("home")=1) & "</td>"
			Response.Write "<td>" & getpstat(dbRS("status")) & "</td>"
			Response.Write "<td><a href=""poll-edit.asp?id=" & dbRS("id") & """>Edit</td>"
			Response.Write "</tr>"
			Response.Write "<tr><td colspan=""5"">"
			Response.Write "<div id=""pr" & dbRS("id") & """ style=""display:none;"">"
			Response.Write "<strong>Poll results</strong>" & vbcrlf
			Response.Write "<table width=""100%"">"
			dbRS2.open "select * from " & db_prefix & "polls_options where pollid = " & dbRS("id") & " order by votes desc",dbConn
			do while not dbRS2.eof
				Response.Write "<tr><td width=""80%"">" & dbRS2("text") & "</td>"
				Response.Write "<td>" & dbRS2("votes") & " votes</td></tr>"
				dbRS2.movenext
			loop
			dbRS2.close
			Response.Write "</table>"
			Response.Write "</div>"
			Response.Write "</td></tr>"
			dbRS.movenext
		loop
	case 8
	'unpublished polls
		do while not(dbRS.eof)
			Response.Write "<tr>"
			Response.Write "<td><a href=""javascript:showhidePR(" & dbRS("id") & ");"">" & dbRS("title") & "</a></td>"
			Response.Write "<td>" & dat2str(dbRS("date")) & "</td>"
			Response.Write "<td>" & (dbRS("home")=1) & "</td>"
			Response.Write "<td>" & getpstat(dbRS("status")) & "</td>"
			Response.Write "<td><a href=""poll-edit.asp?id=" & dbRS("id") & """>Edit</td>"
			Response.Write "</tr>"
			Response.Write "<tr><td colspan=""5"">"
			Response.Write "<div id=""pr" & dbRS("id") & """ style=""display:none;"">"
			Response.Write "<strong>Poll results</strong>" & vbcrlf
			Response.Write "<table width=""100%"">"
			dbRS2.open "select * from " & db_prefix & "polls_options where pollid = " & dbRS("id") & " order by votes desc",dbConn
			do while not dbRS2.eof
				Response.Write "<tr><td width=""80%"">" & dbRS2("text") & "</td>"
				Response.Write "<td>" & dbRS2("votes") & " votes</td></tr>"
				dbRS2.movenext
			loop
			dbRS2.close
			Response.Write "</table>"
			Response.Write "</div>"
			Response.Write "</td></tr>"
			dbRS.movenext
		loop
	case 9
	'expired polls	
		do while not(dbRS.eof)
			Response.Write "<tr>"
			Response.Write "<td><a href=""javascript:showhidePR(" & dbRS("id") & ");"">" & dbRS("title") & "</a></td>"
			Response.Write "<td>" & dat2str(dbRS("date")) & "</td>"
			Response.Write "<td>" & (dbRS("home")=1) & "</td>"
			Response.Write "<td>" & getpstat(dbRS("status")) & "</td>"
			Response.Write "<td><a href=""poll-edit.asp?id=" & dbRS("id") & """>Edit</td>"
			Response.Write "</tr>"
			Response.Write "<tr><td colspan=""5"">"
			Response.Write "<div id=""pr" & dbRS("id") & """ style=""display:none;"">"
			Response.Write "<strong>Poll results</strong>" & vbcrlf
			Response.Write "<table width=""100%"">"
			dbRS2.open "select * from " & db_prefix & "polls_options where pollid = " & dbRS("id") & " order by votes desc",dbConn
			do while not dbRS2.eof
				Response.Write "<tr><td width=""80%"">" & dbRS2("text") & "</td>"
				Response.Write "<td>" & dbRS2("votes") & " votes</td></tr>"
				dbRS2.movenext
			loop
			dbRS2.close
			Response.Write "</table>"
			Response.Write "</div>"
			Response.Write "</td></tr>"
			dbRS.movenext
		loop
end select

Response.Write "<tr><td colspan=""5"">"
if page > 1 then
	Response.Write "<p style=""float:left""><a href=""#"" onclick=""javascript:loadTable('" & host & "','" & ptype & "','" & page-1 & "','" & elem & "','" & rnd() & "');return(false);"">&laquo;&nbsp;Previous page (" & page-1 & ")</a></p>"
end if
Response.Write "<p style=""float:right""><a href=""#"" onclick=""javascript:loadTable('" & host & "','" & ptype & "','" & page+1 & "','" & elem & "','" & rnd() & "');return(false)"">Next page (" & page+1 & ")&nbsp;&raquo;</a></p>"
Response.Write "</td></tr></tbody></table>"

dbRS.close
dbConn.close
set dbRS = nothing
set dbRS2 = nothing
set dbConn = nothing

Sub tableH(h1,h2,h3,h4,h5)
	Response.Write "<table width=""100%"" class=""admin_table"">" & vbcrlf
	Response.Write "<thead><tr>" & vbcrlf
	Response.Write "	<th>" & h1 & "</th>" & vbcrlf
	Response.Write "    <th>" & h2 & "</th>" & vbcrlf
	Response.Write "    <th>" & h3 & "</th>" & vbcrlf
	Response.Write "    <th>" & h4 & "</th>" & vbcrlf
	Response.Write "    <th>" & h5 & "</th>" & vbcrlf
	Response.Write "</tr></thead><tbody>" & vbcrlf
End Sub
%>